package cc.eddic.pratice.programmingparadigms.exam.impl;

import cc.eddic.pratice.programmingparadigms.advanced.BeanFactory;
import cc.eddic.pratice.programmingparadigms.exam.ExamService;
import cc.eddic.pratice.programmingparadigms.exam.MasterDataService;
import cc.eddic.pratice.programmingparadigms.exam.QuestionBankService;
import cc.eddic.pratice.programmingparadigms.exam.domain.Answer;
import cc.eddic.pratice.programmingparadigms.exam.domain.Exam;
import cc.eddic.pratice.programmingparadigms.exam.domain.Question;
import cc.eddic.pratice.programmingparadigms.exam.domain.Student;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ExamServiceGcsStreamImpl implements ExamService {
    @Override
    public List<Answer> importAnswers(String answerExcelFile) {
        MasterDataService masterDataService = BeanFactory.getBean(MasterDataService.class);
        QuestionBankService questionBankService = BeanFactory.getBean(QuestionBankService.class);

        String content = getFileContent();
        List<Answer> answers = new ArrayList<>();
        String[] header = new String[0];

        Exam exam = masterDataService.getExams().stream().filter(x -> "7-12 开营考试".equals(x.getName())).findFirst()
                .orElseThrow(() -> new IllegalArgumentException("找不到 7-12 开营考试"));
        List<Question> questionList = questionBankService.importCorrectAnswers("");
        List<Student> studentList = masterDataService.getStudents();

        for (String line : content.split("\n")) {
            if (log.isDebugEnabled()) log.debug(line);
            String[] values = line.split(",", -1);
            if (log.isDebugEnabled()) log.debug("length : {}", values.length);

            // 表头（包含学生的姓名信息）
            if (header.length == 0) {
                header = values;
                continue;
            }
            if (values.length < 1) {
                continue;
            }

            Question question = null;
            final String questionNo = values[0];
            for (int i = 0; i < values.length; ++i) {
                // 问题
                if (0 == i) {
                    question = questionList.stream().filter(q -> q.getNo() == Integer.parseInt(questionNo)).findFirst()
                            .orElseThrow(() -> new IllegalArgumentException("找不到试题编号为 " + values[0] + " 的试题"));
                    continue;
                }

                // 找学生
                String studentName = header[i];
                Student student = studentList.stream().filter(st -> studentName.equals(st.getName())).findFirst()
                        .orElseThrow(() -> new IllegalArgumentException("找不到学生 " + studentName));

                // 答案可能为空
                char choice = values[i].length() > 0 ? values[i].charAt(0) : ' ';
                if (log.isDebugEnabled()) log.debug("#{} {}-{} {}", questionNo, i, studentName, choice);
                Answer answer = new Answer(exam, question, student, choice);
                answers.add(answer);
            }
            if (log.isDebugEnabled()) log.debug("");
        }
        return answers;
    }

    private String getFileContent() {
        return "试题编号,李惠,谭雨晴,李馨,曾奕哲,车晓满,王巧家,孙晨玮,崔相元,韩睿洁\n" +
                "1,B,B,B,B,B,B,B,B,C\n" +
                "2,C,C,C,C,C,C,C,C,C\n" +
                "3,A,A,A,A,A,B,A,A,A\n" +
                "4,C,C,C,C,C,C,C,B,C\n" +
                "5,D,D,D,D,D,D,D,D,D\n" +
                "6,B,B,B,B,B,D,B,A,B\n" +
                "7,B,B,B,B,B,C,B,C,B\n" +
                "8,A,A,C,A,A,A,A,A,A\n" +
                "9,D,D,D,D,D,D,A,D,D\n" +
                "10,D,B,D,B,D,D,B,D,D\n" +
                "11,B,A,B,C,A,A,A,A,A\n" +
                "12,C,A,C,A,B,C,C,C,C\n" +
                "13,A,A,B,A,A,A,A,A,B\n" +
                "14,A,A,C,C,D,C,A,C,C\n" +
                "15,A,A,A,A,A,B,A,B,A\n" +
                "16,B,B,B,B,B,B,B,B,B\n" +
                "17,A,C,D,D,D,D,D,D,D\n" +
                "18,C,C,C,C,C,C,C,C,C\n" +
                "19,D,D,D,D,D,D,C,A,D\n" +
                "20,B,B,B,B,B,B,B,B,B\n" +
                "21,D,C,C,A,D,D,,A,\n" +
                "22,D,D,D,,D,C,,C,C\n" +
                "23,A,A,A,A,D,D,,C,C\n" +
                "24,C,C,C,C,C,D,A,C,D\n" +
                "25,B,B,B,B,B,B,,D,B";
    }
}
